﻿const pro = require('child_process');

function startup(config) {
    // process.env.PORT = config.port||3001;
    console.log(config)
    // process.chdir(__dirname);
    return run('yarn prd', '启动完成',{
        
        env:{
          PORT:config.port
        }
    });
}

function run(script,name,config = {}) {
    Object.assign(config,{
        cwd:__dirname
    })
    return new Promise((resolve, reject) => {
        let b = pro.exec(script,config, (err) => {
            if (err) {
                reject();
                return;
            }
        })
        b.stdout.on('data', (data) => {
            console.log(data);
        })
        b.stdout.on('end', () => {
            console.log('系统' + name)
            resolve('系统' + name);
            b.kill();
        })
    })
}

function stop() {
    return run('pm2 stop bin/www', '已停止');
}

function build() {
    return run('yarn run build', '构建完成');
}
module.exports.startup = startup;
module.exports.stop = stop;
module.exports.build = build;